import ACCESS_ENUM from "@/access/accessEnum";

/**
 * 用户鉴权、页面显示权限
 * @param loginUser 已登录用户
 * @param needAccess  需要的权限
 * @return  boolean 是否具有权限
 */
const checkAccess = (loginUser: any, needAccess = ACCESS_ENUM.NOT_LOGIN) => {
  //获取当前登录用户的权限
  const loginUserAccess = loginUser?.userRole ?? ACCESS_ENUM.NOT_LOGIN;
  //需要普通用户权限
  if (needAccess === ACCESS_ENUM.USER) {
    if (loginUserAccess === ACCESS_ENUM.NOT_LOGIN) {
      return false;
    }
  }
  //需要管理员权限
  if (needAccess === ACCESS_ENUM.ADMIN) {
    if (loginUserAccess !== ACCESS_ENUM.ADMIN) {
      return false;
    }
  }
  //其余情况鉴权通过
  return true;
};

export default checkAccess;
